<template>
	<a-modal :width='700' v-model:open="open" title="医生指数" @ok="handleOk">
		<div class="rowbox" style='margin-bottom:20px'>
			<a-input style="width: 200px;" v-model="name" placeholder="医生姓名" />
			<a-button style="margin-left: 10px" @click="searchDoctor">医生筛选</a-button>
		</div>
		<div class="rowbox" style='margin-bottom:20px'>
			<a-input style="width: 200px;" v-model="xyzs" placeholder="医生指数" />
			<a-button style="margin-left: 10px" @click="addAll">批量添加</a-button>
		</div>
		<a-table :dataSource="list" :columns="columns">
			<div slot-scope="text, record" slot="xyzs">
				<a-input v-model="record.xyzs" placeholder="请输入医生指数" />
			</div>
		</a-table>
	</a-modal>
</template>

<script>
	import {
		zhishuInfo,
		zhishuInfoUpdate
	} from '@/api/medicine'
	import {
		Modal,
		message
	} from 'ant-design-vue';
	export default {
		data() {
			return {
				name: '',
				xyzs: '',
				id: 0,
				open: false,
				list: [],
				columns: [{
						title: '医生',
						dataIndex: 'name',
						key: 'name',
					},
					{
						title: '西药指数',
						dataIndex: 'xyzs',
						key: 'xyzs',
						scopedSlots: {
							customRender: 'xyzs'
						}
					},
				]
			}
		},
		methods: {
			searchDoctor() {
				this.list.forEach((res, index) => {
					if (res.name == this.name) {
						console.log(res)
						let item = this.list.splice(index, 1)[0]; // 从数组中移除该项并获得它
						this.list.unshift(item);
					}
				})
			},
			// 批量添加
			addAll() {
				this.list.forEach(res => {
					res.xyzs = this.xyzs
				})
			},
			initData(id) {
				this.id = id
				zhishuInfo({
					medicineid: id
				}).then(res => {
					this.open = true
					this.list = res.data
				})
			},
			handleOk() {
				let params = this.list.map(res => {
					return {
						doctorid: res.id,
						xyzs: res.xyzs * 1
					}
				})
				zhishuInfoUpdate({
					medicineid: this.id,
					parameter: JSON.stringify(params)
				}).then(res => {
					message.info('设置成功');
					this.open = false
				})
			}
		}
	}
</script>

<style>
</style>